﻿#include "dataanalysiswindow.h"
#include "ui_dataanalysiswindow.h"
#include "mainwindow.h"


DataAnalysisWindow::DataAnalysisWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DataAnalysisWindow)
{
    ui->setupUi(this);

    MainWindow::isRefDataAnalysis = true;

    pState = nullptr;
    _chartManager = nullptr;
    _table = nullptr;
    _tableGuide = nullptr;
    _actionBar = nullptr;

    _dialog_as = nullptr;
    _dialog_filter = nullptr;

}

DataAnalysisWindow::~DataAnalysisWindow()
{
    delete ui;
}


void DataAnalysisWindow::showEvent(QShowEvent *event)
{

    DebugLog::write("数据分析 - dataAnalysisWindow line34");

    //DataAnalysis da; da.datao();  // 自动导出测试

    if( ! MainWindow::isRefDataAnalysis )  return;
    MainWindow::isRefDataAnalysis = false;
    releaseResource();


    // 创建组件
    _chartManager = new PCRChartManager(this);
    _table = new PCRTable(QStringList() << "孔位" << "样本名称" << "样本类型" << "荧光染料" << "Ct" << "浓度" << "平均浓度", this);
    _tableGuide = new GuideTable2(this);
    _actionBar = new PCRActionBar(this);
    pState = new PCRState();

    _dialog_filter = new DialogFilter2(this);
    _dialog_filter->close();
    _dialog_as = new DialogAnalysisSettings(this);
    _dialog_as->close();

    // 信号绑定
    connect(pState, SIGNAL(sigFilterResultChanged()), _chartManager, SLOT(on_filterResultChanged()));
    connect(pState, SIGNAL(sigBoldResultChanged()),   _chartManager, SLOT(on_boldResultChanged()));

    connect(pState, SIGNAL(sigPageChanged()), _chartManager,    SLOT(on_pageChanged()));
    connect(pState, SIGNAL(sigPageChanged()), _table,           SLOT(on_pageChanged()));
    connect(pState, SIGNAL(sigPageChanged()), _actionBar,       SLOT(on_pageChanged()));
    connect(pState, SIGNAL(sigPageChanged()),  this,            SLOT(on_pageChanged()));
    connect(pState, SIGNAL(sigPageChanged()), _tableGuide,      SLOT(on_pageChanged()));

    connect(pState, SIGNAL(sigFilterChanged()), _table,         SLOT(on_filterChanged()));
    connect(pState, SIGNAL(sigFilterChanged()), _tableGuide,    SLOT(on_filterChanged()));

    connect(pState, SIGNAL(sigThrChanged()), _chartManager,     SLOT(on_thrChanged()));
    connect(pState, SIGNAL(sigThrChanged()), _table,            SLOT(on_thrChanged()));
    connect(pState, SIGNAL(sigThrChanged()), _actionBar,        SLOT(on_thrChanged()));
    connect(pState, SIGNAL(sigThrChanged()), this,              SLOT(on_thrChanged()));

    connect(pState, SIGNAL(sigModelChanged()), _chartManager,   SLOT(on_modelChanged()));
    connect(pState, SIGNAL(sigModelChanged()), _table,          SLOT(on_modelChanged()));
    connect(pState, SIGNAL(sigModelChanged()), _actionBar,      SLOT(on_modelChanged()));

    connect(pState, SIGNAL(sigFilterStart()), this,             SLOT(on_filterStart()));

    connect(pState, SIGNAL(sigStandardChanged()), _table,       SLOT(on_standardChanged()));
    connect(pState, SIGNAL(sigStandardChanged()), _chartManager,SLOT(on_standardChanged()));

    connect(pState, SIGNAL(sigGuideNumChanged()), _actionBar,   SLOT(on_guideNumChanged()));

    connect(pState, SIGNAL(sigGuidePageChanged()), _tableGuide, SLOT(on_guidePageChanged()));

    connect(_dialog_as->_button_yes, SIGNAL(clicked()), this, SLOT(on_reanalysis()));


    // 界面组件布局初始化
    ui->layout_operation->addWidget(_actionBar);
    //ui->layout_chartLabel->addWidget(_chartLegend);

    ui->layout_chart->addWidget(_chartManager);
    ui->layout_table->addWidget(_table);
    ui->layout_table->addWidget(_tableGuide);
    _tableGuide->hide();

    if(MainWindow::experimentStatus.get()==4){
        ui->pushButton_9->show();
        ui->pushButton_10->show();
    }else{
        ui->pushButton_9->hide();
        ui->pushButton_10->hide();
    }

    // 执行数据分析
    if(_boxAnalysis != nullptr){
        delete _boxAnalysis;
        _boxAnalysis = nullptr;
    }
    _boxAnalysis = new QMessageBox(QMessageBox::Question, "提示", "正在执行数据分析,请稍后", QMessageBox::Yes, this);
    _boxAnalysis->show();
    QTimer::singleShot(100, [this](){
        initData();                 // 读取数据
        //datao();
        _boxAnalysis->close();
        _boxAnalysis->hide();
    });

    event->accept();
}


void DataAnalysisWindow::on_pushButton_9_clicked()
{
    this->close();
    emit changeSampleWindow();
}

void DataAnalysisWindow::on_pushButton_10_clicked()
{
    this->close();
    emit changeExperiInfoWindow();
}

void DataAnalysisWindow::on_pushButton_back_clicked()
{
    this->close();
    if(MainWindow::experimentStatus.get()==4){      // 不知道是干嘛的
        MainWindow::experimentStatus.set(0);
    }

    releaseResource();

    emit changeMainWindow();

}

void DataAnalysisWindow::releaseResource(){
    if(pState)          { delete pState;             pState = nullptr;           }
    if(_chartManager)   { delete _chartManager;      _chartManager = nullptr;    }
    if(_table)          { delete _table;             _table = nullptr;           }
    if(_actionBar)      { delete _actionBar;         _actionBar = nullptr;       }
    if(_tableGuide)     { delete _tableGuide;        _tableGuide = nullptr;      }
    if(_dialog_filter)  { delete _dialog_filter;     _dialog_filter = nullptr;   }
    if(_dialog_as)      { delete _dialog_as;         _dialog_as = nullptr;       }
}


// 分析配置
void DataAnalysisWindow::on_pushButton_2_clicked(){
    _dialog_as->exec();
}

void DataAnalysisWindow::on_reanalysis(){


    _dialog_as->close();
    _dialog_as->on_checkChanged();
    if(_boxAnalysis != nullptr){
        delete _boxAnalysis;
        _boxAnalysis = nullptr;
    }
    _boxAnalysis = new QMessageBox(QMessageBox::Question, "提示", "正在执行数据分析,请稍后", QMessageBox::Yes, this);
    _boxAnalysis->show();
    QTimer::singleShot(100, [this](){
        pState->calModel();
        pState->calCt();
        pState->calStandardCurve();
        pState->emitModelChanged();
        pState->emitPageChanged();
        pState->emitFilterChanged();
        pState->emitFilterResultChanged();
        pState->emitThrChanged();
        _boxAnalysis->close();
        _boxAnalysis->hide();
    });
}


// 从数据库读取数据、初始化组件
void DataAnalysisWindow::initData(){

    pState->init();         // 执行数据计算

    _table->init(pState);
    _chartManager->init(pState);
    _actionBar->init(pState);
    _dialog_filter->init(pState);
    _dialog_as->init(pState);
    _tableGuide->init(pState);

    pState->emitModelChanged();
    pState->emitPageChanged();
    pState->emitFilterChanged();
    pState->emitFilterResultChanged();
    pState->emitThrChanged();

}



// pState状态变更响应函数
void DataAnalysisWindow::on_pageChanged(){

    if(pState->currentPage == PCRState::GUIDE_DATA || pState->currentPage == PCRState::GUIDE_PLAN){
        ((QHBoxLayout*)ui->widget_2->layout())->setStretch(0, 0);
        ui->widget_3->hide();
        _table->hide();
        _chartManager->hide();
        _tableGuide->show();
    }else if(pState->currentPage == PCRState::STANDARD){
        ((QHBoxLayout*)ui->widget_2->layout())->setStretch(0, 1);
        ((QHBoxLayout*)ui->widget_2->layout())->setStretch(1, 1);

        ui->widget_3->show();
        _tableGuide->hide();
        _table->show();
        _chartManager->show();
    }
    else{
        ((QHBoxLayout*)ui->widget_2->layout())->setStretch(0, 1);
        ((QHBoxLayout*)ui->widget_2->layout())->setStretch(1, 1);

        ui->widget_3->show();
        _tableGuide->hide();
        _table->show();
        _chartManager->show();
    }
    // 切换diff时需要重新计算阈值
    pState->emitThrChanged();


}

void DataAnalysisWindow::on_filterStart(){
    _dialog_filter->exec();
}
void DataAnalysisWindow::on_thrChanged(){
}


void DataAnalysisWindow::on_pushButton_3_clicked()
{
#ifdef Q_OS_ANDROID
    // QString outpath = "/storage/emulated/0/Download/";
    DialogFileManager dfm(1);
    dfm.exec();
    QString outpath = dfm.getResult();

#else
    QString outpath = QFileDialog::getExistingDirectory(this,
                                                tr("选择文件夹"),
                                                QDir::currentPath(), // 初始目录
                                                QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
#endif
    if(outpath == "0" || outpath==""){
        return;
    }
    QString localPath = datao(outpath);
    DockLabel::I().show("导出成功", this);

}

void DataAnalysisWindow::on_pushButton_exportImage_clicked()
{



#ifdef Q_OS_ANDROID

    DialogFileManager dfm(1);
    dfm.exec();
    QString remotePath = dfm.getResult() + "/pic.png";
    QString localPath = SMBManager::I().SMB2Local(remotePath);
    if(localPath == "0" || localPath == "") return;
    QString filePath = localPath;


#else
    QString filePath = QFileDialog::getSaveFileName(this, "保存", "/path/to/default/directory") +".png";
#endif
    if(filePath.isEmpty()||filePath.isNull()){
        return;
    }
    QPixmap p = _chartManager->_chartView->grab();
    QImage image = p.toImage();
    bool ress = image.save(filePath);


#ifdef Q_OS_ANDROID
    if(SMBManager::isSMBPath(remotePath)){
        bool res = SMBManager::I().uploadFile(remotePath, localPath);

        if(res){
            DockLabel::I().show("导出成功", this);
        }else{
            DockLabel::I().show("導出失敗", this);
        }
    }else{
        if(ress){
            DockLabel::I().show("导出成功", this);
        }else{
            DockLabel::I().show("導出失敗", this);
        }
    }
#endif

}
